import { AudioClass } from "./audio";
import bobmAudio from "./../assets/bobm.mp3";
import { getRandomFloat } from "./unit";

class Particle {
  x;
  y;
  radius;
  color;
  velocity;
  alpha;
  constructor(x, y, radius, color, velocity) {
    this.x = x;
    this.y = y;
    this.radius = radius;
    this.color = color;
    this.velocity = velocity; // [vx, vy]
    this.alpha = 1; // Opacity
  }

  draw(ctx) {
    ctx.save();
    ctx.globalAlpha = this.alpha;
    ctx.beginPath();
    ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, true);
    ctx.closePath();
    ctx.fillStyle = this.color;
    ctx.fill();
    ctx.restore();
  }

  update() {
    this.x += this.velocity[0];
    this.y += this.velocity[1];
    this.alpha -= 0.01;
  }
}

export class Bomb {
  particles = [];
  constructor(x, y) {
    const particles = [];
    for (let i = 0; i < 300; i++) {
      const angle = Math.random() * Math.PI * 2; // Random angle between 0 and 2*PI
      const speed = Math.random() * 5 + 2; // Random speed between 2 and 7 pixels per frame
      const vx = Math.cos(angle) * speed / (innerWidth / 37); // Velocity in x direction
      const vy = Math.sin(angle) * speed / (innerWidth / 37); // Velocity in y direction
      const radius = Math.random() * 5 + 1; // Random radius between 1 and 6 pixels
      const color = `rgba(255, 165, 0, 0.9)`; // Random color with full opacity
      particles.push(new Particle(x, y, radius, color, [vx, vy]));
    }
    this.particles = particles;

    new AudioClass(bobmAudio);
  }
}
